package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Description:
 * @Author: JK
 * @Date: create in 2021/7/21 20:37
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(timeout = 3000, check = false, retries = 0)
    OrderCoreService orderCoreService;

    @Reference(timeout = 3000, check = false, retries = 0)
    OrderQueryService orderQueryService;

    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest servletRequest) {
//        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("uid").toString());

        String uid = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        createOrderRequest.setUserId(Long.parseLong(uid));
        // TODO 发号器
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString().replace("-", ""));

        System.out.println("uid = " + uid);

        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);
        System.out.println("createOrderResponse = " + createOrderResponse);
        if (OrderRetCode.SUCCESS.getCode().equals(createOrderResponse.getCode())) {
            return new ResponseUtil<>().setData(createOrderResponse.getOrderId());
        }

        return new ResponseUtil<>().setErrorMsg(createOrderResponse.getMsg());
    }

    @GetMapping("/order")
    public ResponseData getOrderList(OrderListRequest orderListRequest, HttpServletRequest servletRequest) {
//        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("uid").toString());

        String uid = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        orderListRequest.setUserId(Long.parseLong(uid));

        OrderListResponse orderListResponse = orderQueryService.queryOrderList(orderListRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(orderListResponse.getCode())) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("data", orderListResponse.getDetailInfoList());
            map.put("total", orderListResponse.getTotal());
            return new ResponseUtil<>().setData(map);
        }

        return new ResponseUtil<>().setErrorMsg(orderListResponse.getMsg());
    }

    @GetMapping("/order/{id}")
    public ResponseData queryOrderDetail(@PathVariable String id) {
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);
        OrderDetailResponse2 orderDetailResponse = orderQueryService.queryOrderDetail(orderDetailRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(orderDetailResponse.getCode())) {
            return new ResponseUtil<>().setData(orderDetailResponse);
        }

        return new ResponseUtil<>().setErrorMsg(orderDetailResponse.getMsg());
    }

    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(cancelOrderResponse.getCode())) {
            return new ResponseUtil<>().setData(cancelOrderResponse);
        }

        return new ResponseUtil<>().setErrorMsg(cancelOrderResponse.getMsg());
    }

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable String id) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();

        deleteOrderRequest.setOrderId(id);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(deleteOrderResponse.getCode())) {
            return new ResponseUtil<>().setData(deleteOrderResponse);
        }
        return new ResponseUtil<>().setErrorMsg(deleteOrderResponse.getMsg());
    }
}
